package com.sgb.contract.view.ui.list;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.databinding.ContractActivityContractListBinding;
import com.sgb.contract.model.ContractKeyValueBean;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.contract.view.widget.pop.RejectContractPop;
import com.sgb.contract.view.widget.pop.SelectPop;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

/**
 * 合同列表
 *
 * @author cyj
 */
@Route(path = ArouterConfigs.AR_CONTRACT_LIST)
public class ContractListActivity extends BaseActivity<ContractActivityContractListBinding, ContractViewModel> {

    private SelectPop mSelectPop1, mSelectPop2, mSelectPop3;
    private RejectContractPop rejectContractPop;

    public static void start(Context context) {
        Intent intent = new Intent(context, ContractListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_activity_contract_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ContractViewModel.class, BR.mContractViewModel);
    }


    @Override
    protected void init() {
        mDataBinding.head.title.setText(R.string.name_title_contract_list);
        mDataBinding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp0_5), getResources().getColor(R.color.color_ffededed)));
        mSelectPop1 = new SelectPop(this);
        mSelectPop2 = new SelectPop(this);
        mSelectPop3 = new SelectPop(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mViewModel.getContractList(false);
    }

    @Override
    protected void initObserve() {
        // pop弹窗数据设置
        mViewModel.contractTypeList.observe(this, list -> mSelectPop1.setData(list));
        mViewModel.contractStateList.observe(this, list -> mSelectPop2.setData(list));
        mViewModel.approvalStateList.observe(this, list -> mSelectPop3.setData(list));
        // 第一页无数据显示无数据UI
        mViewModel.hasNoContractListData.observe(this, b -> mViewModel.showNoData(mDataBinding.rlvView));
        mViewModel.showRejectContractPop.observe(this, b -> {
            if (null == rejectContractPop) {
                rejectContractPop = new RejectContractPop(ContractListActivity.this);
            }
            rejectContractPop.showPopwindow(mDataBinding.grayLayout, true);
            rejectContractPop.setRejectContractListener(obj -> {
                // 修改合同
                mViewModel.rejectContractSure(obj);
            });
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {

            @Override
            public void onRefresh() {
                mViewModel.getContractList(false);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getContractList(true);
            }
        });

        // adapter 操作合同按钮点击回调，如：发送合同，修改合同，查看合同等操作按钮
        mViewModel.adapter.setOnclickListener((v, bean) -> {
            mViewModel.checkStatusByNowStatus(String.valueOf(v.getTag()), bean);
        });
        // pop弹窗相关
        mSelectPop1.setOnDismissListener(() -> {
            mDataBinding.imgType.setRotation(0f);
            mViewModel.selectTab.set(0);
            mViewModel.selectTab5.set(0 != mSelectPop1.currentPosition);
            mDataBinding.tvTabContractType.setTextColor(0 == mSelectPop1.currentPosition ?
                    getResources().getColor(R.color.color_ff666666) : getResources().getColor(R.color.color_ffef4033));
        });
        mSelectPop2.setOnDismissListener(() -> {
            mDataBinding.imgStatus.setRotation(0f);
            mViewModel.selectTab.set(0);
            mViewModel.selectTab6.set(0 != mSelectPop2.currentPosition);
            mDataBinding.tvTabContractStatus.setTextColor(0 == mSelectPop2.currentPosition ?
                    getResources().getColor(R.color.color_ff666666) : getResources().getColor(R.color.color_ffef4033));
        });

        mSelectPop3.setOnDismissListener(() -> {
            mDataBinding.imgApproveStatus.setRotation(0f);
            mViewModel.selectTab.set(0);
            mViewModel.selectTab7.set(0 != mSelectPop3.currentPosition);
            mDataBinding.tvTabApproveStatus.setTextColor(0 == mSelectPop3.currentPosition ?
                    getResources().getColor(R.color.color_ff666666) : getResources().getColor(R.color.color_ffef4033));
        });
        mDataBinding.flContractClassification.setOnClickListener(v -> {
            mDataBinding.tvTabContractType.setTextColor(getResources().getColor(R.color.color_ff333333));
            mViewModel.getContractType();
            mViewModel.selectTab.set(1);
            hideSoftInputFromWindow(mDataBinding.etSearch);
            mSelectPop1.showPopwindow(mDataBinding.contractPopLine, 0, DisplayUtil.dip2px(ContractListActivity.this, 1));
        });
        mDataBinding.flContractState.setOnClickListener(v -> {
            mDataBinding.tvTabContractStatus.setTextColor(getResources().getColor(R.color.color_ff333333));
            mViewModel.getContractStatus();
            mViewModel.selectTab.set(2);
            hideSoftInputFromWindow(mDataBinding.etSearch);
            mSelectPop2.showPopwindow(mDataBinding.contractPopLine, 0, DisplayUtil.dip2px(ContractListActivity.this, 1));
        });

        mDataBinding.flApproveState.setOnClickListener(v -> {
            mDataBinding.tvTabApproveStatus.setTextColor(getResources().getColor(R.color.color_ff333333));
            mViewModel.getContractApprovalStatus();
            mViewModel.selectTab.set(3);
            hideSoftInputFromWindow(mDataBinding.etSearch);
            mSelectPop3.showPopwindow(mDataBinding.contractPopLine, 0, DisplayUtil.dip2px(ContractListActivity.this, 1));
        });
        mSelectPop1.setOnItemOnClickListener(new AdapterOnItemClick<ContractKeyValueBean>() {
            @Override
            public void onItemClick(ContractKeyValueBean bean, int position) {
                mViewModel.selectTab5.set(true);
                mDataBinding.imgType.setRotation(180f);
                mDataBinding.tvTabContractType.setText(0 == position ? "合同分类" : bean.value);
                mViewModel.getContractListForType(bean.key);
                mSelectPop1.dismiss();
            }
        });
        mSelectPop2.setOnItemOnClickListener(new AdapterOnItemClick<ContractKeyValueBean>() {
            @Override
            public void onItemClick(ContractKeyValueBean bean, int position) {
                mViewModel.selectTab6.set(true);
                mDataBinding.imgStatus.setRotation(180f);
                mDataBinding.tvTabContractStatus.setText(0 == position ? "合同状态" : bean.value);
                mViewModel.getContractListForStatus(bean.key);
                mSelectPop2.dismiss();
            }
        });

        mSelectPop3.setOnItemOnClickListener(new AdapterOnItemClick<ContractKeyValueBean>() {
            @Override
            public void onItemClick(ContractKeyValueBean bean, int position) {
                mViewModel.selectTab7.set(true);
                mDataBinding.imgApproveStatus.setRotation(180f);
                mDataBinding.tvTabApproveStatus.setText(0 == position ? "审批状态" : bean.value);
                mViewModel.getContractListForApproval(bean.key);
                mSelectPop3.dismiss();
            }
        });

        // 根据合同名称搜索合同
        mDataBinding.etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mViewModel.getContractListForTitle(mDataBinding.etSearch.getText().toString().trim());
            }
            return false;
        });
    }

    @Override
    protected void onDialogOnClick(View dialogView) {
        if (null != dialogView.getTag() && "checkStatusByNowStatus".equals(dialogView.getTag().toString())) {
            mViewModel.getContractList(false);
        } else if (null != dialogView.getTag() && "discardContract".equals(dialogView.getTag().toString())) {
            mViewModel.discardContract();
        } else if (null != dialogView.getTag() && "checkApprovalOpenAudit".equals(dialogView.getTag().toString())) {
            mViewModel.getContractList(false);
        }
    }
}
